<?php

/**
 * @author	barbarosalcin
 * @desc	page_admin_modules
 * @version	$Id: 20110317
 * @package	admin
 */

if ($tk->session->data['user_level'] != ADMIN)
{
	$allowed = 0;
	return;
}

$error = 0;
$filename = basename(__FILE__);
$pagename = substr(substr($filename, 0, - 4), 5, 99);
define('IN_ADMINPAGE', true);

$item_table = MODULES_TABLE;

/**
 * @ignore createtime,status
 */

$item_fields = 'pagemodules';

/**
 * @uses text/imagebrowser/textarea/ckeditor/hidden/file/select/date/checkbox/number/readonly_text/file_upload
 */

$item_array = array(
		'id' => 'hidden', 
		
		'pagename' => 'readonly_text', 
		'pagemodules1' => 'select', 
		'pagemodules2' => 'select', 
		'pagemodules3' => 'select', 
		'pagemodules4' => 'select', 
		
		'arr' => array());

$xmodules = array(
		'1' => array(
				'0' => $lang['select_null']), 
		'2' => array(
				'0' => $lang['select_null']), 
		'3' => array(
				'0' => $lang['select_null']), 
		'4' => array(
				'0' => $lang['select_null']));

$sql = "SELECT SQL_CACHE id,title,adverttype from " . ADVERTS_TABLE . " 
where status = 1 AND (expiretime = 0 OR expiretime > '{$config['time']}') order by adverttype asc";
if (! ($result = $tk->db->sql_query($sql)))
{
	$error = 'sql';
	goto end;
}

while($row = $tk->db->sql_fetchrow($result))
{
	switch ($row['adverttype'])
	{
		/* Leaderboard (728 x 90) */
		case '1' :
			$xmodules['1'][$row['id']] = $row['title'];
		break;
		/*Banner (468 x 60)*/
		case '2' :
			$xmodules['2'][$row['id']] = $row['title'];
		break;
		
		/* Small Square (200x200) */
		case '3' :
			$xmodules['3'][$row['id']] = $row['title'];
		break;
		/*Banner (230 x 200) - only for index page */
		case '4' :
			$xmodules['4'][$row['id']] = $row['title'];
		break;
		default :
		break;
	}
}

foreach ( $xmodules as $key => $value )
{
	foreach ( $value as $skey => $val )
	{
		$item_array['arr']['pagemodules' . $key][$skey] = $val;
	}
}

/* default ! do not edit */
$item_fields_additem = preg_replace('/(\w+)/i', '\'\$$1\'', $item_fields);
$item_fields_manitem = preg_replace('/(\w+)/i', '$1=\'\$$1\'', $item_fields);

if ((int) $_POST['submit'])
{
	include ('pages/admin/ajax_' . $pagename . '.php');
	return;
}

/* get list */

if (! empty($setmodules))
{
	//$modules[$pagename]['additem'] = $req['language'] . '-' . $pagename . $config['seoext'] . '?act=additem';
	$modules[$pagename]['manitem'] = $req['language'] . '-' . $pagename . $config['seoext'] . '?act=manitem';
	return;
}

if ($req['get']['act'] == 'additem')
{
	
	/* default ! do not edit */
	$tk->page->_pv['items'] = $item_array;
	$tk->page->_pv['data']['act'] = $req['get']['act'];
	$tk->page->_pv['data'][$req['get']['act']] = $req['language'] . '-' . $pagename . '.html?';

}

if ($req['get']['act'] == 'manitem')
{
	$order_by = "pagename asc";
	$sql = "SELECT SQL_CACHE id,pagename from $item_table order by $order_by";
	
	if (! ($result = $tk->db->sql_query($sql)))
	{
		$error = 'sql';
		goto end;
	}
	$row = $tk->db->sql_fetchrowset($result);
	
	$tk->page->_pv['data']['list'] = $row;
	
	/* default ! do not edit */
	$tk->page->_pv['items'] = $item_array;
	$tk->page->_pv['data']['act'] = $req['get']['act'];
	$tk->page->_pv['data'][$req['get']['act']] = $req['language'] . '-' . $pagename . '.html?';
	$tk->page->_pv['data_json'] = preg_replace('%\[(.+)\]%', '$1', json_encode($data_json));
}

if ($req['get']['act'] == 'getitem')
{
	$id = (int) $req['get']['id'];
	
	$sql = "SELECT SQL_CACHE id,pagename,$item_fields from $item_table where id = '$id'";
	
	if (! ($result = $tk->db->sql_query($sql)))
	{
		$error = 'sql';
		goto end;
	}
	
	$data_json = $tk->db->sql_fetchrow($result);
	$tmp = $data_json['pagemodules'];
	unset($data_json['pagemodules']);
	
	preg_match_all('/"(\w+)":"(\w+)"/i', $tmp, $result, PREG_PATTERN_ORDER);
	
	for($i = 0; $i < count($result[1]); $i++)
	{
		$data_json[$result[1][$i]] = $result[2][$i];
	}
	
	unset($tmp);
	unset($result);
	
	$tk->page->_pv['json'] = 1;
	$tk->page->_pv['data_json'] = preg_replace('%\[(.+)\]%', '$1', json_encode($data_json));

}

if ($req['get']['act'] == 'delitem')
{
	$id = (int) $req['get']['id'];
	
	$sql = "delete from $item_table where id = '$id'";
	
	if (! ($result = $tk->db->sql_query($sql)))
	{
		$error = 'sql';
		goto end;
	}
	
	$data_json = array(
			'header' => $lang['success_header'], 
			'message' => $lang[$pagename][$req['get']['act']]['success'], 
			'status' => '1');
	$tk->page->_pv['json'] = 1;
	$tk->page->_pv['data_json'] = json_encode($data_json);

}

if ($req['get']['act'] == 'toggle')
{
	$id = (int) $req['get']['id'];
	$status = (int) $req['get']['t'];
	
	$sql = "update $item_table set status= '$status' where id='$id'";
	
	if (! ($result = $tk->db->sql_query($sql)))
	{
		$error = 'sql';
		goto end;
	}
	$data_json = array(
			'status' => '1');
	
	$tk->page->_pv['json'] = 1;
	$tk->page->_pv['data_json'] = json_encode($data_json);
}


end: